package com.example.mycamera;

import java.io.File;
import java.io.IOException;

import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CommonCamera extends Activity implements OnClickListener{
    ImageView imageView;
    Button button;
    private static final int IMAGE_CODE=200;
    private File currentFile=null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_common_camera);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		imageView=(ImageView) findViewById(R.id.imageView1);
		button=(Button) findViewById(R.id.button2);
		button.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.common_camera, menu);
		return true;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		File dir=new File(Environment.getExternalStorageDirectory(),"myphoto");
		if(!dir.exists()){
			dir.mkdirs();
		}
		currentFile=new File(dir, System.currentTimeMillis()+".jpg");
		if(!currentFile.exists()){
			try{
				currentFile.createNewFile();
			}catch (IOException e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		Intent in=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
		in.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(currentFile));
		startActivityForResult(in, IMAGE_CODE);
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		Log.d("TAG", requestCode + "\n" + resultCode);
		if (requestCode == IMAGE_CODE) {
			if (resultCode == RESULT_OK) {
				imageView.setImageURI(Uri.fromFile(currentFile));
			}else{
				currentFile.delete();
			}
		}
	}
	

}
